package com.jy.modules.cims.component.imagesign;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.sysUtils.FileUtils;
import com.jy.modules.cims.common.sysUtils.SimpleStringUtils;
import com.jy.modules.cims.component.image.TransferImagesHelper;
import com.jy.modules.cims.data.interact.api.response.ResponseAttachSearchDTO;
import com.jy.modules.cims.sysUtils.ImgurFtpConfigInfo;

@Service("com.jy.modules.cims.component.imagesign.ImagesSignervice")
public class ImagesSignervice {

	private static Logger logger = LoggerFactory.getLogger(ImagesSignervice.class);
	
	@Autowired
	@Qualifier("com.jy.modules.cims.component.image.TransferImagesHelper")
	private TransferImagesHelper transferImagesHelper;
	

	/**
	 * 向上--上传处理影像文件
	 * @author zhangmi
	 * @date  2016年12月30日
	 * @param transImagesSignDto
	 * @return
	 */
	public List<TransReturnUrlDataDto> doImages(final TransImagesSignDto transImagesSignDto){
		
		ImgurFtpConfigInfo imgurFtpConfigInfo = null;//FTP配置信息
		String downLocalPath = null; //临时目录
		
		ResponseAttachSearchDTO responseAttachSearchDto = transImagesSignDto.getResponseAttachSearchDto();
		
		try {
			logger.info("向上-下载影像服务开始！");
			/**
			 * 从向上FTP服务器下载
			 */
			imgurFtpConfigInfo = transferImagesHelper.getImgurFtpConfigInfoByCooperorgCode(transImagesSignDto.getCooperorgCode());

			//创建临时目录
			transferImagesHelper.createTempDir(imgurFtpConfigInfo.getTmpFilePath());
			
			//下载
			downLocalPath = transferImagesHelper.downLoadFTPImg(imgurFtpConfigInfo, responseAttachSearchDto);
			if(!SimpleStringUtils.hasText(downLocalPath)){
				logger.info("合同编号：【{}】，文件下载失败！！",responseAttachSearchDto.getId());
				return null;
			}
			//获取文件名字
			String[] attachUrlArray = responseAttachSearchDto.getFilePath().split("/");
			String remoteFileName = attachUrlArray[attachUrlArray.length - 1];// 待下载文件名称
			boolean unZipFlag = transferImagesHelper.unZipFile(transferImagesHelper.getFullLoadUrl(downLocalPath, remoteFileName), downLocalPath);
			if(!unZipFlag){
				logger.info("合同编号：【{}】，文件解压失败！",responseAttachSearchDto.getId());
				return null;
			}
			//获取文件list
			List<String> listFilePaths = new ArrayList<String>();
			transferImagesHelper.getFilesByDirectory(new File(downLocalPath), listFilePaths);
			logger.info("向上-获取需要上线影像信息："+listFilePaths.size());
			//获取每个文件对应的信息
			List<TransReturnUrlDataDto> listFileTmp = new ArrayList<TransReturnUrlDataDto>();
			transferImagesHelper.getCompareFiles(listFilePaths, responseAttachSearchDto.getFile(), listFileTmp);
			
			//上传文件
			listFileTmp = transferImagesHelper.uploadFileKey(listFileTmp);

			logger.info("向上-下载影像服务结束！"+listFileTmp.size());
			return listFileTmp;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("合同编号：【{}】，文件上传失败！",responseAttachSearchDto.getId());
		}finally{
			FileUtils.deleteDirectory(downLocalPath);
		}
		return null;
	}
}
